from flask import render_template, request, url_for
from application.decorators import login_required, admin_required
from google.appengine.ext import db
from application.models import *
from application.reader import *
from application.views.cron import q_mark_as_read
from google.appengine.api import users
from google.appengine.api import taskqueue

class Viewer:
    folder = ''
    cont_urlarg = ''
    item_list = []

@login_required
def view():
    maggic_req = request.args.get('m')
    readed_list = request.args.getlist('r')
    tag_channel_str = request.args.get('k')

    user_key = verify_update_user()
    for readed in readed_list:
        taskqueue.add(url='/q_mark_as_read', params={'user_key': str(user_key), 'item_key':readed})

    gen = gen_reading_list(maggic_req, tag_channel_str)
    url_args = 'm=' + gen['maggic_str']
    for item in gen['list']:
        url_args += '&r='
        url_args += item.key

    viewer = Viewer()
    viewer.folder = gen['folder']
    viewer.cont_urlarg = url_args
    viewer.item_list = gen['list']
    return render_template('view.html', viewer=viewer)

@login_required
def subscribe():
    new_feed = request.args.get('add')
    if new_feed != None:
        add_channel(new_feed)

    gen = gen_subscribe_list()
    resp = ''
    for title in gen:
        if title != None:
            resp += title
            resp += '\n'
    return resp

@login_required
def folders():
    return 'Ok'